<template>
  <a-card :bordered="false">

    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="8">
            <a-form-item label="流程名称">
              <a-input placeholder="请输入流程名字" v-model="queryParam.processName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="流程编码">
              <a-input placeholder="请输入流程编码" v-model="queryParam.processKey"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="流程类型">
              <j-dict-select-tag v-model="queryParam.processType" dict-code="bpm_process_type" />
            </a-form-item>
          </a-col>

          <a-col :md="6" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新建流程</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :scroll="{ x: true }"
        :components="components"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange">

        <span slot="action" slot-scope="text, record">
          <a-upload :showUploadList="false"
                    :action="uploadAction(record.id)"
                    :data="{'isup':1}"
                    :headers="tokenHeader"
                    @change="handleChange">
             <a>上传流程</a>
          </a-upload>
          <a-divider type="vertical" />
          <a @click="handleEdit(record)">设计流程</a>
          <a-divider type="vertical" />
          <a @click="handleProcessConfig(record)">表单配置</a>
          <a-divider type="vertical" />
          <a @click="handlePublish(record.id)">发布</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item v-if="record.processStatus === 0">
                <a-popconfirm title="确定删除流程吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
              <a-menu-item v-if="record.processStatus === '1'">
                  <a @click="handleDeploymentList(record)">版本监控</a>
              </a-menu-item>
               <a-menu-item>
                  <a @click="copyProcess(record.id)">流程复制</a>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 流程设计区域 -->
    <add-process-modal ref="addProcessModal" @ok="modalFormOk"></add-process-modal>
    <!-- 流程配置区域 -->
    <ext-act-process-form-modal ref="extActProcessFormModal"></ext-act-process-form-modal>
    <!-- 版本监控区域 -->
    <ext-act-process-deployment-modal ref="extActProcessDeploymentModal"></ext-act-process-deployment-modal>
    <!-- 表单图标设置-->
    <ext-act-process-icon-modal ref="extActProcessIconModal" @ok="modalFormOk"></ext-act-process-icon-modal>
    <!--流程设计-->
    <ExtActProcessModal ref="extActProcessModal" @ok="modalFormOk" ></ExtActProcessModal>
  </a-card>
</template>

<script>
  import AddProcessModal from './modules/AddProcessModal'
  import ExtActProcessModal from './modules/ExtActProcessModal'
  import ExtActProcessFormModal from './modules/ExtActProcessFormModal'
  import {getAction,putAction } from '@/api/manage'
  import ExtActProcessDeploymentModal from "./modules/ExtActProcessDeploymentModal";
  import ExtActProcessIconModal from "./modules/ExtActProcessIconModal";
  import {initDictOptions, filterDictText} from '@/components/dict/JDictSelectUtil'
  import {JeecgListMixin} from '@/mixins/JeecgListMixin'

  export default {
    name: "ExtActProcessList",
    mixins: [JeecgListMixin],
    components: {
      ExtActProcessDeploymentModal,
      AddProcessModal,
      ExtActProcessFormModal,
      ExtActProcessIconModal,
      ExtActProcessModal
    },
    data () {
      return {
        description: '流程设计管理',
        // 查询条件
        queryParam: {},
        processTypeDictOptions:[],
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width: 60,
            align:"center",
            sorter: true,
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title: '流程名称',
            align:"center",
            sorter: true,
            dataIndex: 'processName',
            width: 250,
            ellipsis: true,

          },
          {
            title: '流程编码',
            align:"center",
            sorter: true,
            width: 250,
            ellipsis: true,
            dataIndex: 'processKey',
          },
          {
            title: '当前版本号',
            align:"center",
            sorter: true,
            width: 250,
            ellipsis: true,
            dataIndex: 'processVersion',
          },
          {
            title: '流程类型',
            align:"center",
            sorter: true,
            width: 200,
            ellipsis: true,
            dataIndex: 'processType',
            customRender: (text, record, index) => {
              //字典值替换通用方法
              return filterDictText(this.processTypeDictOptions, text);
            }
          },
          {
            title: '发布状态',
            align:"center",
            sorter: true,
            width: 200,
            ellipsis: true,
            dataIndex: 'processStatus',
            customRender:function (text) {
              if(text==1){
                return "已发布";
              }else if(text==0){
                return "未发布";
              }else{
                return text;
              }
            }
          },
          {
            title: '创建时间',
            align:"center",
            sorter: true,
            width: 200,
            ellipsis: true,
            dataIndex: 'createTime',
          },
          {
            title: '操作',
            dataIndex: 'action',
            width: 360,
            ellipsis: true,
            align:"center",
            fixed: 'right',
            sorter: true,
            scopedSlots: { customRender: 'action' },
          }
        ],
        url: {
          list: "/process/extActProcess/list",
          delete: "/process/extActProcess/delete",
          deleteBatch: "/process/extActProcess/deleteBatch",
          deployProcess: "/process/extActProcess/deploy",
          uploadProcess: "/process/extActProcess/uploadProcess",
          copyProcess: "/process/extActProcess/copyProcess",
        },

      }
    },
    methods: {
      loadTable(data){
        this.loadData();
      },
      initDictConfig() {
        //初始化字典 - 性别
        initDictOptions('bpm_process_type').then((res) => {
          if (res.success) {
            this.processTypeDictOptions = res.result;
          }
        });
      },
      handlePublish: function(id){
        var that = this;
        this.$confirm({
          title:"确认发布流程",
          content:"是否发布该流程?",
          onOk: function(){
            putAction(that.url.deployProcess, { id: id }).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.loadData();
              } else {
                that.$message.warning(res.message);
              }
            })
          }
        });
      },
      handleProcessConfig:function(record){
        this.$refs.extActProcessFormModal.show(record.id);
      },
      handleEdit: function(record){
        this.$refs.extActProcessModal.open(record.id,record.processName);
      },
      handleAdd: function(){
        this.$refs.addProcessModal.add();
      },
      handleDeploymentList: function(record){
        var processKey = record.processKey;
        var processName = record.processName;
        this.$refs.extActProcessDeploymentModal.deploymentList(processKey);
        this.$refs.extActProcessDeploymentModal.title="流程【"+processName+"】版本监控";
      },
      uploadAction:function (id) {
        return window._CONFIG['domianURL'] + this.url.uploadProcess+"?id="+id;
      },
      handleChange(info) {
        if (info.file.status === 'done') {
          if (info.file.response.success) {
            this.$message.success(`流程上传成功`);
          } else {
            this.$message.error(`${info.file.name} ${info.file.response.message}.`);
          }
        } else if (info.file.status === 'error') {
          this.$message.error(`流程上传失败: ${info.file.msg} `);
        }
      },
      copyProcess: function(id){
        var that = this;
        this.$confirm({
          title:"确认复制流程",
          content:"是否复制该流程?",
          onOk: function(){
            getAction(that.url.copyProcess, { id: id }).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.loadData();
              } else {
                that.$message.warning(res.message);
              }
            })
          }
        });
      },
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>
